package com.linwen.excise.leetcode;

/**
 * @ClassName _169多数元素
 * @Description TODO
 * @Author zero
 * @DATE 2024/4/7 3:05 AM
 * @Version 1.0
 */
public class _169多数元素 {
    public static void main(String[] args) {
        int[] arr = {2,2,1,1,1,2,2};
        System.out.println(majorityElement(arr));
    }
    public static int majorityElement(int[] nums) {
        int result = nums[0];
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            if(nums[i] == result){
                count++;
            }else {
                if(count == 0){
                    result = nums[i];
                    count = 1;
                }else {
                    count--;
                }
            }
        }
        return result;
    }
}
